package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.bean.Tag;
import com.hhs.storage.dao.TagDao;
import com.hhs.storage.service.TagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hhs
 * @since 2023-03-30
 */
@Service
public class
TagServiceImpl extends ServiceImpl<TagDao, Tag> implements TagService {
    @Resource
    private HttpServletRequest request;

    /**
     * 查询标签
     *
     * @param name
     * @param type
     * @return
     */
    @Override
    public Map queryList(String userId, String name, String type) {

        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (StringUtils.isNotEmpty(type)) {
            queryWrapper.eq("type", type);
        }
        if (StringUtils.isEmpty(userId)) {
            userId = SysUtil.getLoginUserId(request);
        }
        queryWrapper.eq("creator_id", userId);//查询当前用户自己的
        queryWrapper.orderByAsc("create_time");
        List<Tag> list = this.list(queryWrapper);
        Comparator<Tag> colorComparator = new Comparator<Tag>() {

            @Override
            public int compare(Tag colorObj1, Tag colorObj2) {

                String color1 = colorObj1.getColor();
                String color2 = colorObj2.getColor();

                if (StringUtil.iszNull(color1)) {
                    return 1;
                } else if (StringUtil.iszNull(color2)) {
                    return -1;
                } else if (StringUtil.iszNull(color1) && StringUtil.iszNull(color2)) {
                    return 0;
                }

                return color1.compareTo(color2);
            }
        };

        Collections.sort(list, colorComparator);

        return ResponseInfo.success(list);
    }

    /**
     * 新增标签
     *
     * @param name
     * @param type
     * @return
     */
    @Override
    public Map add(String userId, String name, String type, String color) {

        if (SysUtil.TAG_TYPE_MAP.get(type) == null) {
            return ResponseInfo.error("错误的标签类型");
        }
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("type", type);
        if (StringUtils.isEmpty(userId)) {
            userId = SysUtil.getLoginUserId(request);
        }
        queryWrapper.eq("creator_id", userId);//查询当前用户自己的
        queryWrapper.orderByAsc("create_time");
        List<Tag> list = this.list(queryWrapper);
        if (list.size() > 0) {
            return ResponseInfo.error("标签已存在");
        }
        Tag tag = new Tag();
        tag.setName(name);
        tag.setType(type);
        tag.setColor(color);
        tag.setCreateTime(LocalDateTime.now());
        tag.setCreatorId(userId);
        this.save(tag);
        return ResponseInfo.success("新增成功");
    }

    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @Override
    public Map delete(Integer id) {
        this.removeById(id);
        return ResponseInfo.success("删除成功");
    }
}
